/*
 @description: 管理员登录
*/
'use strict';
const Controller = require('egg').Controller;
const utility = require("utility")
class TokenController extends Controller {
    async logout() {
        const {
            ctx,
            app
        } = this;
        ctx.body = {
            code: 20000,
            data: "success"
        }

    }
    async index() {
        const {
            ctx,
            app
        } = this;
        const {
            username,
            password
        } = ctx.request.body;
        console.log(username, password)

        if (username && password) {
            // console.log(5555555555)
            const user = await app.mysql.query(`select name from adminuser`)
            const result = user.find(item => item.name === username);
            if (result) {

                let pwd = await app.mysql.query(`select password from adminuser 
                where name='${result.name}'`)

                console.log(utility.md5(pwd[0].password), utility.md5(password));
                if (utility.md5(pwd[0].password) === utility.md5(password)) {
                    console.log(9999)
                    const adminInfo = {
                        username,
                        password,
                    };
                    const token = app.jwt.sign(adminInfo, app.config.jwt.sercet)
                    ctx.body = {
                        code: 0,
                        msg: "登录成功",
                        token,
                        adminInfo
                    }

                } else {
                    console.log(555555555)
                    ctx.body = {
                        code: 4004,
                        msg: '账号或密码错误'
                    }

                }
            } else {
                ctx.body = {
                    code: 4000,
                    msg: '格式不正确'
                }
            }
        }
    }
    async create() {
        const {
            ctx
        } = this;
        console.log(666666666)
        ctx.body = {
            code: 4000,
            msg: '格式不正确'
        }
    }
}
module.exports = TokenController